﻿using System;
using System.Web.UI;
using Components.Data.UI.Controls.ControlBuilders;

namespace Components.Data.UI.Controls
{
  public class ControlBinding
  {
    internal ControlBinding(Control control, String parentControlID)
    {
      Control = control;
      ParentControlID = parentControlID;
    }

    public String ParentControlID { get; private set; }

    public Control Control { get; protected set; }

    public Boolean BindControl(Control control)
    {
      if (this.Control == null)
        return false;

      Control parentControl = ControlService.FindControl(control, this.ParentControlID);
      if (parentControl == null)
        return false;

      parentControl.Controls.Add(this.Control);
      return true;
    }

    public Boolean BindChildControl(ControlBinding controlBinding)
    {
      if (this.Control == null)
        return false;

      Control parentControl = ControlService.FindControl(this.Control, controlBinding.ParentControlID);
      if (parentControl == null)
        return false;

      controlBinding.BindControl(parentControl);
      return true;
    }
  }
}